package libs

import (
	"fmt"
	"html/template"
	"regexp"
	"strconv"
	"strings"
	"time"
)

//时间戳转换为日期
func TimeToDate(t time.Time) string {
	if t.IsZero() {
		return ""
	}
	return t.Format("2006-01-02 15:04:05")
}

func StrToHtml(content string) template.HTML {
	return template.HTML(content)
}

func NumToStr(num uint) string {
	return strconv.Itoa(int(num))
}

func FormatLen(str string) string {
	text_len := len([]rune(str))
	if text_len <= 5 {
		return str
	}
	strRune := []rune(str)
	return string(strRune[:5])
}

func Print(data interface{}) interface{} {
	fmt.Println(data)
	return data
}

func TrimHtml(src string) string {
	//将HTML标签全转换成小写
	re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
	src = re.ReplaceAllStringFunc(src, strings.ToLower)
	//去除STYLE
	re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
	src = re.ReplaceAllString(src, "")
	//去除SCRIPT
	re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
	src = re.ReplaceAllString(src, "")
	//去除所有尖括号内的HTML代码，并换成换行符
	re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
	src = re.ReplaceAllString(src, "\n")
	//去除连续的换行符
	re, _ = regexp.Compile("\\s{2,}")
	src = re.ReplaceAllString(src, "\n")

	src = strings.Replace(src, "&nbsp;", "", -1)
	return strings.TrimSpace(src)
}
